﻿using huqiang.Framework;
using System;
using System.Collections.Generic;
using System.IO;

public class LZMAFileManager
{
    /// <summary>
    /// 同时运行的最大线程数
    /// </summary>
    public static int ThreadCount = 3;
    static List<LZMAMission> LMissions = new List<LZMAMission>();
    public static int MissionCount { get => LMissions.Count; }
    public static LZMAMission GetCurrentMission()
    {
        if (LMissions.Count > 0)
            return LMissions[0];
        return null;
    }
    public static void Decode(string filepath, string filename, string version, string block, string savedic,Action<IMission> done)
    {
        if (File.Exists(filepath))
        {
            for (int i = 0; i < LMissions.Count; i++)
            {
                if (LMissions[i].filePath == filepath)
                    return;
            }
            LZMAMission lz = new LZMAMission();
            lz.Name = filename;
            lz.Version = version;
            lz.filePath = filepath;
            lz.savePath = savedic+"/" + filename;
            lz.Block = block;
            lz.Completed = done;
            LMissions.Add(lz);
        }
    }
    public static void Update()
    {
        int c = LMissions.Count;
        if (c > ThreadCount)
            c = ThreadCount;
        for (int i = 0; i < c; i++)
        {
            if (!LMissions[i].Running)
                LMissions[i].Run();
        }
        if (LMissions.Count > 0)
        {
            var m = LMissions[0];
            if (!m.Running)
                m.Run();
            if (m.Done)
            {
                LMissions.RemoveAt(0);
                if (m.Completed != null)
                    m.Completed(m);
            }
            else
            {
                if (m.Tip != null)
                    m.Tip(m);
            }
        }
    }
    public static void Clear()
    {
        LMissions.Clear();
    }
}
